﻿Imports Microsoft.VisualBasic

Public Class B03_checkBudgetLimitActual
    Public Shared Function GetActual(ByVal FundCenter As Object, ByVal Commitment_Item As Object, ByVal ActivityType As Object, ByVal Product As Object, ByVal Project As Object, ByVal PlanYear As Object) As String
        If IsDBNull(FundCenter) = True Then
            FundCenter = ""
        End If
        If IsDBNull(Commitment_Item) = True Then
            Commitment_Item = ""
        End If
        If IsDBNull(ActivityType) = True Then
            ActivityType = ""
        End If
        If IsDBNull(Product) = True Then
            Product = ""
        End If
        If IsDBNull(Project) = True Then
            Project = ""
        End If
        If IsDBNull(PlanYear) = True Then
            PlanYear = ""
        End If


        Dim actual As String = ""
        'Dim Year As Integer = Val(PlanYear) - 1
        Dim adap As New CheckBudgetLimitTableAdapters.BudgetDetails1TableAdapter
        Dim FunctionalArea As String = ""
        If ActivityType <> "" Then
            FunctionalArea = ActivityType
        ElseIf Product <> "" Then
            FunctionalArea = Product

        End If

        Dim sql As String = ""
        sql += " SELECT ISNULL(SUM(Actual1 + Actual2 + Actual3 + Actual4 + Actual5),0) "
        sql += " AS Actual FROM BudgetDetails "
        sql += " WHERE Version = 'X01' AND FundCenter_FundCenterCode = '" & FundCenter & "' AND PlanYear = " & Val(PlanYear)
        If Commitment_Item <> "" Then
            sql += " AND Cmmt_CmmtCode = '" & Commitment_Item & "'"
        End If
        If Project <> "" Then
            sql += " AND Project_ProjectCode = '" & Project & "'"
        End If

        If FunctionalArea <> "" Then
            sql += " AND FunctionalArea = '" & FunctionalArea & "'"
        End If
        Dim actval = adap.GetActualData(sql)
        actual = Format(actval, "###,##0.00")
        'actual = Val(adap.getActual("B01", FundCenter, Commitment_Item, Project, Val(PlanYear), FunctionalArea))

        Return actual
    End Function

    Public Shared Function GetPreYear(ByVal FundCenter As Object, ByVal Commitment_Item As Object, ByVal ActivityType As Object, ByVal Product As Object, ByVal Project As Object, ByVal PlanYear As Object) As String
        If IsDBNull(FundCenter) = True Then
            FundCenter = ""
        End If
        If IsDBNull(Commitment_Item) = True Then
            Commitment_Item = ""
        End If
        If IsDBNull(ActivityType) = True Then
            ActivityType = ""
        End If
        If IsDBNull(Product) = True Then
            Product = ""
        End If
        If IsDBNull(Project) = True Then
            Project = ""
        End If
        If IsDBNull(PlanYear) = True Then
            PlanYear = ""
        End If

        Dim PreYear As String = ""
        'Dim Year As Integer = Val(PlanYear) - 1
        Dim adap As New CheckBudgetLimitTableAdapters.BudgetDetails1TableAdapter
        Dim FunctionalArea As String = ""
        If ActivityType <> "" Then
            FunctionalArea = ActivityType
        ElseIf Product <> "" Then
            FunctionalArea = Product

        End If

        Dim sql As String = ""
        sql += " SELECT ISNULL(SUM(Actual1 + Actual2 + Actual3 + Actual4 + Actual5 + Actual6 + Actual7 + Actual8 + Actual9 + Actual10 + Actual11 + Actual12 + Actual13 + Actual14 + Actual15 + Actual16),0) "
        sql += " AS Actual FROM BudgetDetails "
        sql += " WHERE Version = 'B01' AND FundCenter_FundCenterCode = '" & FundCenter & "' AND PlanYear = " & Val(PlanYear) - 1
        If Commitment_Item <> "" Then
            sql += " AND Cmmt_CmmtCode = '" & Commitment_Item & "'"
        End If
        If Project <> "" Then
            sql += " AND Project_ProjectCode = '" & Project & "'"
        End If

        If FunctionalArea <> "" Then
            sql += " AND FunctionalArea = '" & FunctionalArea & "'"
        End If
        Dim preval = adap.GetActualData(sql)
        PreYear = Format(preval, "###,##0.00")
        'PreYear = Val(adap.getActual("A01", FundCenter, Commitment_Item, Project, Val(PlanYear), FunctionalArea))
        Return PreYear
    End Function

End Class

Namespace CheckBudgetLimitTableAdapters
    Partial Public Class BudgetDetails1TableAdapter
        Inherits System.ComponentModel.Component

        Public Function GetActualData(ByVal SQL As String) As Decimal
            'Dim text1 As String
            'text1 = Me.CommandCollection(2).CommandText
            'Try
            Me.CommandCollection(2).CommandText = SQL
            Return Me.getActual2
        End Function
    End Class
End Namespace